/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package application;

import controleur.ConnexionBD;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 *
 * @author James
 */
public class LivreDAO {
    private static PreparedStatement pstmt;
    private static ResultSet results;
    
    public static ResultSet getLivres() {
        final String query = "select * from livre order by titre";
        
        try {          
            pstmt = ConnexionBD.creerConnexion().prepareStatement(query);
            results = pstmt.executeQuery();
            
        } catch (Exception e) {
            return null;
        }
        return results;
    }
    
    public static ResultSet getLivre(final String isbn) {
        final String query = "select * from livre where isbn=?";
        
        try {          
            pstmt = ConnexionBD.creerConnexion().prepareStatement(query);
            pstmt.setString(1, isbn);
            results = pstmt.executeQuery();
            
        } catch (Exception e) {
            return null;
        }
        return results;
    }
    
    public static ResultSet getLivresAuteurListe(final String idAuteur) {
        final String query = "select * from livre l join auteur_livre al on l.isbn=al.isbn "
                + "join auteur a on al.id_auteur=a.id_auteur where a.id_auteur=? "
                + "order by titre";
        
        try {          
            pstmt = ConnexionBD.creerConnexion().prepareStatement(query);
            pstmt.setInt(1, Integer.parseInt(idAuteur));
            results = pstmt.executeQuery();
            
        } catch (Exception e) {
            return null;
        }
        return results;
    }
    
    public static ResultSet getLivresCommande(final String idCommande) {
        final String query = "select * from livre l join emprunt e on l.isbn=e.isbn "
                + "join commande c on e.id_commande=c.id_commande where c.id_commande=? "
                + "order by titre";
        
        try {          
            pstmt = ConnexionBD.creerConnexion().prepareStatement(query);
            pstmt.setInt(1, Integer.parseInt(idCommande));
            results = pstmt.executeQuery();
            
        } catch (Exception e) {
            return null;
        }
        return results;
    }
}
